package com.lczyfz.istep.modules.openstack.api.neutron.impl.firewall;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.firewall.FireWallPolicies;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWallPolicy;
import com.lczyfz.istep.modules.openstack.api.neutron.model.firewall.FireWallRuleInsert;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class FireWallPolicyImpl implements FireWallPolicies {

	public RequestFactory factory = new RequestFactory("neutron");
	@Override
	public FireWallPolicy insertFireWallRule(String FwPolicyId, FireWallRuleInsert insertrule) {
		FireWallPolicy fwpolicy = new FireWallPolicy();
		JSONObject params = NeutronParse.packageFireWallRuleInster(insertrule);
		System.out.println(params.toString());
	//	System.out.println("/v2.0/fwaas/firewall_rules/" + FwRuleId);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_policies/" + FwPolicyId+"/insert_rule");
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		fwpolicy = NeutronParse.resolveFireWallPolicyDetail(res);
		return fwpolicy;
	}

	@Override
	public FireWallPolicy deleteFireWallRule(String FwPolicyId, String FwRuleId) {
		FireWallPolicy fwpolicy = new FireWallPolicy();
		JSONObject params = NeutronParse.packageFireWallRuleRemove(FwRuleId);
	//	System.out.println(params.toString());
	//	System.out.println("/v2.0/fwaas/firewall_rules/" + FwRuleId);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_policies/"+FwPolicyId+"/remove_rule"  );
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		fwpolicy = NeutronParse.resolveFireWallPolicyDetail(res);
		return fwpolicy;
	}

	@Override
	public List<FireWallPolicy> getFireWallPolicies() {
		List<FireWallPolicy> fwpolicies = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_policies");
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
//		System.out.println(body.toString());
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}	
		
		fwpolicies = NeutronParse.resolveFireWallPolicies(body);
		return fwpolicies;
	}

	@Override
	public FireWallPolicy getFireWallPolicy(String fireWallPolicyId) {
		FireWallPolicy fwpolicy = new FireWallPolicy();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_policies/" + fireWallPolicyId);
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
	//	System.out.println(body.toString());
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		fwpolicy = NeutronParse.resolveFireWallPolicyDetail(body);
		return fwpolicy;
	}

	@Override
	public FireWallPolicy createFireWallPolicy(FireWallPolicy fireWallPolicy) {
		FireWallPolicy fwpolicy = new FireWallPolicy();
		JSONObject params = NeutronParse.packageFireWallPolicy(fireWallPolicy);
		System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_policies");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		
		Response results = HTTP.getConnectionResult(request);	
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		System.out.println(res.toString());
		fwpolicy = NeutronParse.resolveFireWallPolicyDetail(res);
		return fwpolicy;
	}

	@Override
	public void deleteFireWallPolicy(String fireWallPolicyId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_policies/" + fireWallPolicyId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
	}

	@Override
	public FireWallPolicy updateFireWallPolicy(String fireWallPolicyId, FireWallPolicy fireWallPolicy) {
		FireWallPolicy fwpolicy = new FireWallPolicy();
		JSONObject params = NeutronParse.packageFireWallPolicy(fireWallPolicy);
		System.out.println(params.toString());
	//	System.out.println("/v2.0/fwaas/firewall_rules/" + FwRuleId);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/fw/firewall_policies/" + fireWallPolicyId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		fwpolicy = NeutronParse.resolveFireWallPolicyDetail(res);
		return fwpolicy;
	}
	
	public static void main(String[] args) {
		FireWallPolicyImpl fwpolicy = new FireWallPolicyImpl();
		fwpolicy.deleteFireWallPolicy("eb1f3b25-58a7-4aad-ad5f-4d358be04702");
		List<FireWallPolicy> fwpolicylist = fwpolicy.getFireWallPolicies();
		for(FireWallPolicy fp : fwpolicylist) {
			System.out.println(fp.getId()+" "+fp.getAudited()+" "+fp.getName()+" "+fp.getProjectId()+" "+fp.getShared()+" "+fp.getTenantId()
			+" "+fp.getFirewallRules());
		}
//		
//		FireWallPolicy fwpolicydetail = fwpolicy.getFireWallPolicy("eb1f3b25-58a7-4aad-ad5f-4d358be04702");
//		System.out.println(fwpolicydetail.getId()+" "+fwpolicydetail.getAudited()+" "+fwpolicydetail.getName()+" "+
//		fwpolicydetail.getProjectId()+" "+fwpolicydetail.getShared()+" "+fwpolicydetail.getTenantId()
//		+" "+fwpolicydetail.getFirewallRules());
//		
//		FireWallPolicy fwpolcydemo = new FireWallPolicy();
//		fwpolcydemo.setAudited("false");
//		fwpolcydemo.setName("policy5");
//		fwpolcydemo.setShared("true");
//		FireWallPolicy newfwpolicy =  fwpolicy.createFireWallPolicy(fwpolcydemo);
////		
//		FireWallPolicy updatefwpolicy = new FireWallPolicy();
//		updatefwpolicy.setAudited("true");
//		updatefwpolicy.setName("policy2");
//		ArrayList<String> rule = new ArrayList<String>();
//		rule.add("06a2cef3-9fc6-436a-9e0f-e7d0cc61ed88");
//		updatefwpolicy.setFirewallRules(rule);
//		FireWallPolicy newpolicy = fwpolicy.updateFireWallPolicy("eb1f3b25-58a7-4aad-ad5f-4d358be04702", updatefwpolicy);
////				
//		fwpolicy.deleteFireWallPolicy("d36a1f3a-9f26-4548-a168-1add743446ae");
//		
//		try {
//			FireWallRuleInsert insertrules = new FireWallRuleInsert();
//			insertrules.setFwRuleId("06a2cef3-9fc6-436a-9e0f-e7d0cc61ed88");
//			FireWallPolicy insertrule = fwpolicy.insertFireWallRule("eb1f3b25-58a7-4aad-ad5f-4d358be04702",insertrules);
//		}catch(Exception e) {
//			
//		}
//		try {
//			FireWallPolicy removerule = fwpolicy.deleteFireWallRule("eb1f3b25-58a7-4aad-ad5f-4d358be04702", "06a2cef3-9fc6-436a-9e0f-e7d0cc61ed88");
//	
//		}catch(Exception e) {
//			
//		}
	}

}
